<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label='开始时间:'>
              <a-date-picker v-model:value='where.searchBeginTime' format='YYYY-MM-DD' valueFormat='YYYY-MM-DD' />
            </a-form-item>
            <a-form-item label='结束时间:'>
              <a-date-picker v-model:value='where.searchEndTime' format='YYYY-MM-DD' valueFormat='YYYY-MM-DD' />
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="dctdId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          v-model:selection="selection"
          :scroll="{ x: 'max-content' }"
        >

          <!-- table上边工具栏 -->
          <template #toolbar>
            <a-space>
              <a-button type="primary" @click="openSetHeartbeat()">
                <template #icon>
                  <plus-outlined />
                </template>
                <span>批量设置网关的握手时间</span>
              </a-button>
              <a-button  type="primary" @click="openGetSim()">
                <template #icon>
                  <plus-outlined />
                </template>
                <span>批量获取SIM卡信息</span>
              </a-button>
            </a-space>
          </template>

          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="openDetail(record)">详情</a>
                <a-divider type="vertical"  />
                <div >
                  <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                    <a class="ele-text-danger">删除</a>
                  </a-popconfirm>
                </div>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <DevicePowerMonitorFirmwareCommandTemplateDistributeDetail v-if='showDetail' v-model:visible="showDetail" :data="current"  />
    <DevicePowerMonitorFirmwareCommandTemplateDistributeHeartbeat v-if='showHeartbeat' v-model:visible="showHeartbeat" :data="current" @done="reload"  />
    <DevicePowerMonitorFirmwareCommandTemplateDistributeGetsim v-if='showGetSim' v-model:visible="showGetSim" :data="current" @done="reload"  />
  </div>
</template>

<script>
import { DeviceCommandTemplateDistributeApi } from '@/api/property/DeviceCommandTemplateDistributeApi';
import { message } from 'ant-design-vue';
import  DevicePowerMonitorFirmwareCommandTemplateDistributeDetail from './powermonitor-firmware-command-template-distribute-detail.vue'
import DevicePowerMonitorFirmwareCommandTemplateDistributeHeartbeat from './powermonitor-firmware-command-template-distribute-heartbeat.vue'
import DevicePowerMonitorFirmwareCommandTemplateDistributeGetsim from './powermonitor-firmware-command-template-distribute-getsim.vue'
export default {
  name: 'DevicePowerMonitorFirmwareCommandTemplateDistribute',
  components: {
    DevicePowerMonitorFirmwareCommandTemplateDistributeDetail,
    DevicePowerMonitorFirmwareCommandTemplateDistributeHeartbeat,
    DevicePowerMonitorFirmwareCommandTemplateDistributeGetsim
  },
  async mounted() {
  },
  props: {
    data: Object
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '分配的设备ID',
          dataIndex: 'recordIds'
        },
        {
          title: '推送类型',
          dataIndex: 'sceneDictate',
          customRender: function(row) {
            if (row.text == 0) {
              return "推送时间到终端";
            } else if (row.text == 4) {
              return "重启终端设备";
            } else if (row.text == 5) {
              return "通知设备更新代码";
            } else if (row.text == 6) {
              return "更新终端网关的握手间隔时间";
            } else if (row.text == 7) {
              return "设置终端连接服务器的IP和端口";
            } else if (row.text == 25) {
              return "设置参数(全局配置和各种参数配置)"
            } else if (row.text == 32) {
              return "读取设备时间";
            } else if (row.text == 37) {
              return "读取终端网关的握手间隔时间";
            } else if (row.text == 56) {
              return "读取参数配置";
            } else if (row.text == 34) {
              return "读取设备id和固件版本";
            } else if (row.text == 38) {
              return "读取设备的SIM卡信息";
            }
          }
        },
        {
          title: '分发状态',
          dataIndex: 'isDistribute',
          customRender: function(row) {
            const isDistribute = row.text;
            if (isDistribute == 0) {
              return '未分发';
            } else if (isDistribute == 1) {
              return '分发中';
            } else if (isDistribute == 2) {
              return '分发成功，无失败';
            } else if (isDistribute == 3) {
              return '分发成功，存在失败';
            }
          }
        },
        {
          title: '设备总数',
          dataIndex: 'total'
        },
        {
          title: '成功数',
          dataIndex: 'distributeSuccess'
        },
        {
          title: '失败数',
          dataIndex: 'distributeFail'
        },
        {
          title: '执行信息',
          dataIndex: 'failMsg'
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center',
        }
      ],
      // 表格搜索条件
      where: {
        templateCode : this.data.templateCode
      },
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showDetail: false,
      showHeartbeat : false,
      showGetSim : false
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2024/10/03 21:26
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2024/10/03 21:26
     */
    reset() {
      this.where.templateCode = this.data.templateCode;
      this.where.searchBeginTime = '';
      this.where.searchEndTime = '';
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2024/10/03 21:26
     */
    async remove(row) {
      const result = await DeviceCommandTemplateDistributeApi.delete({ dctdId: row.dctdId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2024/10/03 21:26
     */
    openDetail(row) {
      this.current = row;
      this.showDetail = true;
    },
    openSetHeartbeat(row) {
      this.current = row;
      this.showHeartbeat = true;
    },
    openGetSim(row) {
      this.current = row;
      this.showGetSim = true;
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return DeviceCommandTemplateDistributeApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }
  }
};
</script>
